package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.ExhibitionDetailsModel;
import com.zw.yzk.kabu.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/2/6.
 */

public class TicketPropertyAdapter extends RecyclerView.Adapter<TicketPropertyAdapter.Holder> {

    private List<ExhibitionDetailsModel.Property> dataList;
    private Context context;

    public TicketPropertyAdapter(List<ExhibitionDetailsModel.Property> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            this.dataList.addAll(list);
        }
        if (dataList.isEmpty()) {
            return;
        }
        for (ExhibitionDetailsModel.Property property : dataList) {
            if (property.isSelected()) {
                property.setSelected(false);
            }
        }
        dataList.get(0).setSelected(true);
    }

    public ExhibitionDetailsModel.Property getSelectedProperty() {
        for (ExhibitionDetailsModel.Property property : dataList) {
            if (property.isSelected()) {
                return property;
            }
        }
        return null;
    }

    public List<ExhibitionDetailsModel.Property> getDataList() {
        return dataList;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_ticket_property_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final ExhibitionDetailsModel.Property model = dataList.get(position);
        holder.type.setText(model.getContent());
        holder.price.setText(ResUtils.formatCurrency(context, model.getOriginalPrice()));
        holder.type.setChecked(model.isSelected());

        holder.type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (ExhibitionDetailsModel.Property property : dataList) {
                    property.setSelected(false);
                }
                dataList.get(holder.getAdapterPosition()).setSelected(true);
                if (listener != null) {
                    listener.onClicked(model);
                }
                notifyDataSetChanged();
//                int position = holder.getAdapterPosition();
//                if (dataList.get(position).isSelected()) {
//                    return;
//                }
//                for (int i = 0; i < dataList.size(); i++) {
//                    if (i == position) {
//                        if (listener != null) {
//                            listener.onClicked(model);
//                        }
//                        dataList.get(i).setSelected(true);
//                        notifyItemChanged(i);
//                    } else if (dataList.get(i).isSelected()) {
//                        dataList.get(i).setSelected(false);
//                        notifyItemChanged(i);
//                    }
//                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.type)
        RadioButton type;
        @Bind(R.id.price)
        TextView price;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnPropertySelectedListener {
        void onClicked(ExhibitionDetailsModel.Property property);
    }

    private OnPropertySelectedListener listener;

    public void setOnPropertySelectedListener(OnPropertySelectedListener listener) {
        this.listener = listener;
    }
}
